package com.abu.crms.controller;


import com.abu.crms.config.mvc.LoginInterceptor;
import com.abu.crms.pojo.Manager;
import com.abu.crms.service.ManagerService;
import com.abu.crms.config.common.Resp;
import com.abu.crms.vo.ManagerVO;
import com.abu.crms.vo.QueryConditionVO;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

/**
 * <p>
 * 管理员表（超级管理员，楼主） 前端控制器
 * </p>
 *
 * @author 阿布
 * @since 2021-02-03
 */
@RestController
@RequestMapping("/crms/manager")
public class ManagerController {


    @Autowired
    private ManagerService managerService;

    @PostMapping("auth")
    public Resp auth(
            @RequestParam("mgName") String mgName,
            @RequestParam("mgPwd") String mgPwd) {
        HashMap<String, Object> data = this.managerService.auth(mgName, mgPwd);
        return Resp.ok().data(data);
    }

    @GetMapping("queryManagerByCondition")
    public Resp queryByCondition(QueryConditionVO queryCondition) {
        HashMap<String, Object> mapResult = this.managerService.queryByCondition(queryCondition);
        return Resp.ok().data(mapResult);
    }

    @PostMapping("addManager")
    public Resp addManager(@RequestBody ManagerVO manager) {
        Boolean flag = this.managerService.saveManagerAndRole(manager);
        if (flag) {
            return Resp.ok();
        }
        return Resp.error().msg("添加失败");
    }


    @PutMapping("updateManagerStatistics")
    public Resp updateManagerStatistics(@RequestBody Manager manager) {
        boolean flag = this.managerService.updateById(manager);
        if (!flag) {
            return Resp.error().msg("修改失败");
        }
        return Resp.ok();
    }

    @PutMapping("updateStatus")
    public Resp changeStatus(@RequestParam("status") Boolean status, @RequestParam(value = "managerId") Long managerId) {
        if (managerId == null) {
            return Resp.error().msg("数据为空");
        }
        UpdateWrapper<Manager> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("mg_id", managerId).set("mg_state", status);
        boolean flag = this.managerService.update(updateWrapper);
        Manager manager = this.managerService.getById(managerId);
        Boolean presentStatus = manager.getMgState();
        String userId = LoginInterceptor.getManagerId();
        System.out.println(userId);
        if (flag) {
            return Resp.ok().data("status", presentStatus);
        }
        return Resp.error().msg("修改状态失败");
    }

    @PutMapping("resetPassword")
    public Resp resetPwd(@RequestParam("mgId") Long mgId) {
        Boolean flag = this.managerService.resetPwd(mgId);
        if (flag) {
            return Resp.ok().msg("密码已重置为：a987456");
        }
        return Resp.error().msg("重置失败");
    }


    @DeleteMapping("deleteById")
    public Resp deleteManagerById(@RequestParam("managerId") Long managerId) {
        if (managerId == null) {
            return Resp.error().msg("数据为空");
        }
        boolean flag = this.managerService.removeManagerAndRole(managerId);
        if (!flag) {
            return Resp.error().msg("修改失败");
        }
        return Resp.ok();
    }

    @DeleteMapping("batchDelete")
    public Resp batchDelete(String ids) {
        Boolean flag = this.managerService.deleteBatch(ids);
        if (!flag) {
            return Resp.error().msg("删除失败");
        }
        return Resp.ok();
    }


}

